<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<title></title>
		<link rel="stylesheet" type="text/css" href="../pages/bill/css/index.css" />
		<!--<script type="text/javascript" src="../lib/jquery.min.js"></script>
		<script type="text/javascript" src="../lib/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../lib/easyui-lang-zh_CN.js"></script>-->
		<script src="../pages/bill/js/jquery-1.11.3.min.js" type="text/javascript" charset="utf-8"></script>
		<!-- <script type="text/javascript" src="../js/thor.js?v=${version}"></script> -->
		<script type="text/javascript" src="../lib/standalone-framework.js"></script>
		<script src="../pages/bill/layer/layer.js" type="text/javascript" charset="utf-8"></script>
	</head>

	<body>
		<div class="container">
			<!--logo图片文字-->
			<div class="logoview">
				<img src="../pages/bill/img/login_logo_wanke.png" />
				<h3>万科商业地产移动管理软件</h3>
			</div>

			<!--信息输入-->
			<div class="inputview">
				<ul>
					<li class="active" id="tenantlogin">商户登录</li>
					<li id="marketlogin">内部登录</li>
				</ul>
				<div class="input-username inputtxt">
					<label>
						<img src="../pages/bill/img/user_icon.png"/>
						<input type="text" name="username" id="username" maxlength="20" placeholder="请输入用户名" />
					</label>
				</div>
				<div class="input-password inputtxt">
					<label>
						<img src="../pages/bill/img/pwd_icon.png"/>
						<input type="password" name="password" id="password" maxlength="20" placeholder="请输入密码" />
					</label>
				</div>

				<a href="javascript:;" class="loginBtn" id="loginBtn">登&nbsp;录</a>

			</div>
			<div class="companyInfo">上海海鼎信息工程股份有限公司&nbsp;&nbsp;<span>v1.6.27</span></div>
		</div>
		<!--<script src="../pages/bill/js/login.js" type="text/javascript" charset="utf-8"></script>-->
		<script type="text/javascript">
			$(function() {
				//初始化，检查是否有保存登录数据
				function init() {
					var userInfo = JSON.parse(localStorage.getItem('userInfo')) || {};
					if(userInfo) {
						$('#username').val(userInfo.principal);
						$('#password').val(userInfo.password);
						if(userInfo.type == 0) {
							merchantLogin(userInfo.principal, userInfo.password)
						} else if(userInfo.type == 1) {
							insideLogin(userInfo.principal, userInfo.password)
						}
					}
				}
				init();

				$('.inputview ul li').click(function() {
					$(this).addClass('active').siblings().removeClass('active');
				})
				//商户登录
				function merchantLogin(user, pwd) {
					layer.load(2);
					var authen = {
						principal: user,
						password: pwd
					}
					var userdata = {
						principal: user,
						password: pwd,
						type: 0
					}
					var d = JSON.stringify(userdata);
					$.ajax({
						url: "tenantlogin.thor",
						data: authen,
						cache: false,
						dataType: 'json', //服务器返回json格式数据
						type: 'post', //HTTP请求类型
						timeout: 10000, //超时时间设置为10秒；
						success: function(data) {
							console.log(data)
							layer.closeAll('loading');
							layer.msg("登录成功");
							var sessionContracts = [];//存储用户协议
							for(var i=0; i<data.stores.length; i++){
								var item = {};
								if(data.stores[i].contracts.length>0){
									item = {
										code:data.stores[i].contracts[0].code,
										name:data.stores[i].contracts[0].name,
										uuid:data.stores[i].contracts[0].uuid,
									}
									sessionContracts.push(item);
								}
							}
							window.sessionStorage.setItem('sessionContracts',JSON.stringify(sessionContracts));//存储用户协议
							var dat = JSON.stringify(data);
							window.localStorage.setItem('loginInfo', dat);
							window.localStorage.setItem('userInfo', d);
							window.location.href = "tobill.thor";
						},
						error: function(data) {
							layer.closeAll('loading');
							if(data.readyState == 4){
								layer.msg(data.responseJSON.exceptionMessage);
							}else{
								layer.msg("网络错误...");
							}
						}
					});
				}
				//内部登录
				function insideLogin(user, pwd) {
					layer.load(2);
					var authen = {
						principal: user,
						password: pwd
					}
					var userdata = {
						principal: user,
						password: pwd,
						type: 1
					}
					var d = JSON.stringify(userdata);
					$.ajax({
						url: "marketlogin.thor",
						data: authen,
						cache: false,
						dataType: 'json', //服务器返回json格式数据
						type: 'post', //HTTP请求类型
						timeout: 10000, //超时时间设置为10秒；
						success: function(data) {
							console.log(data);
							layer.closeAll('loading');
							var data = JSON.stringify(data);
							window.localStorage.setItem("loginInfo", data);
							window.localStorage.setItem('userInfo', d);
							window.location.href = "tobill.thor";
							
						},
						error: function(data) {
							layer.closeAll('loading');
							if(data.readyState == 4){
								layer.msg(data.responseJSON.exceptionMessage);
							}else{
								layer.msg("网络错误...");
							}

						}
					});
				}
				//点击登录按钮事件
				$('#loginBtn').click(function() {
					var userName = $('#username').val(); //获取用户名
					var userPass = $('#password').val(); //获取密码
					var type = 0; //登录类别
					var reg = new RegExp('active');
					if(reg.test($('.inputview ul li').eq(0).attr('class'))) {
						type = 0;
						console.log(type)

					} else if(reg.test($('.inputview ul li').eq(1).attr('class'))) {
						type = 1;
						console.log(type)

					}
					if(userName == '' || userPass == '') {
						layer.msg("用户名或密码为空");
					} else if(type == 0) {
						merchantLogin(userName, userPass);
					} else if(type == 1) {
						insideLogin(userName, userPass);
					}
				})
				
			})
		</script>
	</body>

</html>